﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using AddAnswertoDatabaseWebApplication.InsertAnswerDataSetTableAdapters;
using System.ServiceModel.Activation;

namespace AddAnswertoDatabaseWebApplication
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "InsertAnswerDataByClientsService" in code, svc and config file together.
    public class InsertAnswerDataByClientsService : IInsertAnswerDataByClientsService
    {
        ManageAnswerByClientTableAdapter mabcta = new ManageAnswerByClientTableAdapter();
        QuestionSetNameTableAdapter qsnta = new QuestionSetNameTableAdapter();
        QuestionNameTableAdapter qnta = new QuestionNameTableAdapter();
        ManageQuestionTableAdapter mqta = new ManageQuestionTableAdapter();

        public string InsertAnswerToDatabase(string clientId, string questionSetName, string questionName, string answer)
        {
            
            try
            {
                for (int i = 0; i < qsnta.GetQuestionSetByQuestionSetName(questionSetName).Count; i++)
                {
                    int questionSetId = qsnta.GetQuestionSetByQuestionSetName(questionSetName)[i].Id;
                    for (int j = 0; j < qnta.GetQuestionNameByQuestionName(questionName).Count; j++)
                    {
                        int questionId = qnta.GetQuestionNameByQuestionName(questionName)[j].Id;
                        if (mqta.GetDataByQuestionIdAndQuestionSetId(questionId, questionSetId).Count > 0)
                        {
                            mabcta.Insert(questionSetId, questionId, Convert.ToInt16(clientId), answer);
                             return "Success";
                        }
                    }
                }
               
            }
            catch (Exception)
            {

                return "Failed";
            }
            return "Failed";
      
        }
    }
}
